package com.qnkf.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.*;

/**
 * @author: QIK
 * @CreateDate: 2019/4/15 17:09
 */
public class DateUtil {
    private static Map<Integer ,String>  monthMap=new HashMap<Integer, String>(0){{
        put(1,"January");
        put(2,"February");
        put(3,"March");
        put(4,"April");
        put(5,"May");
        put(6,"June");
        put(7,"July");
        put(8,"August");
        put(9,"Sept");
        put(10,"October");
        put(11,"November");
        put(12,"December");
    }};



    /**
     * 根据当前时间，添加或减去指定的时间量。例如，要从当前日历时间减去 5 天，可以通过调用以下方法做到这一点：
     * add(Calendar.DAY_OF_MONTH, -5)。
     *
     * @param date 指定时间
     * @param num  为时间添加或减去的时间天数
     * @return
     */
    public static Date getBeforeOrAfterDate(Date date, int num) {
        //获取日历
        Calendar calendar = Calendar.getInstance();
        //当date的值是当前时间，则可以不用写这段代码。
        calendar.setTime(date);
        calendar.add(Calendar.DATE, num);
        //把日历转换为Date
        return calendar.getTime();
    }


    /**
     * 当前日
     * @return
     */
    public static String currentDay(){
        Calendar cal = Calendar.getInstance();
        Integer day=cal.get(Calendar.DATE);
        if(day<10){
            return "0"+day;
        }
        return day+"";
    }

    /**
     * 当前月
     * @return
     */
    public static String currentMonth(){
        Calendar cal = Calendar.getInstance();
        Integer month=cal.get(Calendar.MONTH) + 1;
        return monthMap.get(month);
    }

    /**
     * 当前年
     * @return
     */
    public static String currentYear(){
        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.YEAR)+"";
    }

    /**
     * 获取两个时间相差的秒数
     * @param date1
     * @param date2
     * @return
     */
    public static Long getDifferTime(Date date1,Date date2){
        long time1 = date1.getTime();
        long time2 = date2.getTime();
        return time1-time2;
    }


    /**
     * 获取当前时间
     * Thu Apr 08 10:45:20 CST 2021
     * @return
     */
    public static Date now(){
        LocalDateTime localDateTime = LocalDateTime.now();
        return Date.from( localDateTime.atZone( ZoneId.systemDefault()).toInstant());
    }


    /**
     * 获取当天毫秒值
     * @return
     */
    public static long getStartTimeOfDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 字符转日期
     * Mon Apr 05 11:21:12 CST 2021
     * @param strDate
     * @return
     */
    public static Date strToDateLong(String strDate) {
       SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
     }

    /**
     * 获取当前时间
     *2021-04-08 10:51:00
     * @return
     */
    public static String getCurrentTime() {
        GregorianCalendar g = new GregorianCalendar();
        return dateToString(g);
    }

    /**
     * 获取当前日期
     * 2021-04-08
     * @return
     */
    public static String getCurrentDate() {
        GregorianCalendar g = new GregorianCalendar();
        return dateToStringSim(g);
    }

    /**
     * 获取昨天日期
     *
     * @return
     */
    public static String getYesterdays() {
        GregorianCalendar g = new GregorianCalendar();
        g.set(Calendar.DATE, g.get(Calendar.DATE) - 1);
        return dateToString(g);
    }

    /**
     * 获取前 nday天日期
     *
     * @return
     */
    public static String getndays(int nday) {
        GregorianCalendar g = new GregorianCalendar();
        g.set(Calendar.DATE, g.get(Calendar.DATE) - nday);
        return dateToString(g);
    }

    /**
     * 获取前 nday天日期 并格式化
     *
     * @return
     */
    public static String getNdays(int nday) {
        GregorianCalendar g = new GregorianCalendar();
        g.set(Calendar.DATE, g.get(Calendar.DATE) - nday);
        return dateTString(g);
    }

    /**
     * 获取前 nday天日期 并格式化
     *
     * @return
     */
    public static long getCurrentTimeMillis(int nday) {
        Long a = System.currentTimeMillis();
        long b = nday * 24 * 60 * 60 * 1000;
        return a - b;
    }

    /**
     * 获取 nMillis分钟后的时间 并格式化
     *
     * @return
     */
    public static String getNewMillis(long a,int nMillis) {
        long b = nMillis * 60000;
        return getTime(a + b);
    }

    /**
     * 字符串转时间戳
     * @return
     */
    public static long dateToStamp(String str){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

    public static long getDayStamp(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        return DateUtil.dateToStamp(df.format(new Date()));
    }

    /**
     * data -> String
     * @param date
     * @return
     */
    public static String formatDate(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(date);
    }

    /**
     * String -> data
     * @param str
     * @return
     * @throws ParseException
     */
    public static Date parseDate(String str) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.parse(str);
    }

    /**
     * data -> String
     * @param date
     * @return
     */
    public static String formatYMDDate(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }


    /**
     * 获得早上六点时间
     * 如果 当前时间大于6点则获得今天6点的时间
     * 否则获得昨天6点的时间
     *
     * @return
     */
    public static Date get6HourDays() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        int curHour24 = c.get(Calendar.HOUR_OF_DAY);
        int dd = 6;
        if (curHour24 < dd) { //获得前一天日期
            c.add(Calendar.DATE, -1);
        }
        c.set(Calendar.HOUR_OF_DAY, dd);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 获得早上24点时间
     * 如果 当前时间大于24点则获得今天6点的时间
     * 否则获得昨天24点的时间
     *
     * @return
     */
    public static Date get24HourDays() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        int curHour24 = c.get(Calendar.HOUR_OF_DAY);
        int dd = 24;
        if (curHour24 < dd) { //获得前一天日期
            c.add(Calendar.DATE, -1);
        }
        c.set(Calendar.HOUR_OF_DAY, dd);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 获得当月的第一天时间
     *
     * @return
     */
    public static Date getMonthHourDays() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        int curHour24 = c.get(Calendar.HOUR_OF_DAY);
        int dd = 24;
//        if (curHour24 < dd) { //获得前一天日期
//            c.add(Calendar.DATE, -1);
//        }
        c.set(Calendar.DATE, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 获得这个星期的第一天时间
     *
     * @return
     */
    public static Date getWeekHourDays() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        int curHour24 = c.get(Calendar.HOUR_OF_DAY);
        int dd = 24;
        c.set(Calendar.DAY_OF_WEEK, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 获取昨天日期
     *
     * @return
     */
    public static String getYesterday() {
        GregorianCalendar g = new GregorianCalendar();
        g.set(Calendar.DATE, g.get(Calendar.DATE) - 1);
        return dateToStringSim(g);
    }

    /**
     * 获取今天日期
     *
     * @return
     */
    public static String getToday() {
        GregorianCalendar g = new GregorianCalendar();
        g.set(Calendar.DATE, g.get(Calendar.DATE));
        return dateToStringSim(g);
    }


    /**
     * 获取明天日期
     *
     * @return
     */
    public static String getTomorrow() {
        GregorianCalendar g = new GregorianCalendar();
        g.set(Calendar.DATE, g.get(Calendar.DATE) + 1);
        return dateToStringSim(g);
    }

    /**
     * 获取当前时间-分
     *
     * @return
     */
    public static int getNewTime() {
        Calendar now = Calendar.getInstance();

        return now.get(Calendar.MINUTE);
//		System.out.println("年：" + now.get(Calendar.YEAR));
//
//		System.out.println("月：" + (now.get(Calendar.MONTH) + 1));
//
//		System.out.println("日：" + now.get(Calendar.DAY_OF_MONTH));
//
//		System.out.println("时：" + now.get(Calendar.HOUR_OF_DAY));
//
//		System.out.println("分：" + now.get(Calendar.MINUTE));
//
//		System.out.println("秒：" + now.get(Calendar.SECOND));
    }

    /**
     * 转换成时间
     *
     * @param ms
     * @return
     */
    public static String getTime(long ms) {
        GregorianCalendar g = new GregorianCalendar();
        g.setTimeInMillis(ms);
        return dateToString(g);
    }

    /**
     * 转换成时间
     *
     * @param ms
     * @return
     */
    public static String getTimeMillis(long ms) {
        GregorianCalendar g = new GregorianCalendar();
        g.setTimeInMillis(ms);
        return dateToString(g);
    }


    /**
     * 根据时间获取GregorianCalendar
     * "yyyy-MM-dd HH:mm:ss"
     * @param time
     * @return
     */
    private static GregorianCalendar getCalendarByTime(String time, String format) {
        GregorianCalendar g = new GregorianCalendar();
        try {
            g.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time));
        } catch (ParseException e) {
            g = null;
        }
        return g;
    }

    /**
     * 计算时间
     *
     * @param g
     * @param seconds
     * @return
     */
    private static String calcDate(GregorianCalendar g, int seconds) {
        if (g == null) {
            g = new GregorianCalendar();
        }
        g.add(Calendar.SECOND, seconds);
        return dateToString(g);
    }

    /**
     * 日期转字符串
     *
     * @param date
     * @return
     */
    public static String dateToStrings(Date date) {
        try {
            DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String s = formatter.format(date);
            return s;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 日期转字符串
     *
     * @param date
     * @return
     */
    public static String dateToString(Date date) {
        try {
            DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String s = formatter.format(date);
            return s;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 字符串转日期
     *
     * @param date
     * @return
     */
    public static Date stringToDate(String date) {
        try {
            DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            Date d = (Date) formatter.parse(date);
            return d;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 日期转字符串
     *
     * @param
     * @return
     */
    public static Date getDateString() {
        try {
            DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date d = (Date) formatter.parse(DateUtil.dateToStrings(new Date()));
            return d;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 日期转字符串
     *
     * @param date
     * @return
     */
    public static String getDateToString(Date date) {
        try {
            DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String s = formatter.format(date);
            return s;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 字符串转日期
     *
     * @param date
     * @return
     */
    public static Date stringToDates(String date) {
        try {
            DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date d = (Date) formatter.parse(date);
            return d;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 字符串转日期, 自定义格式
     *
     * @param date
     * @param format
     * @return
     */
    public static Date stringToDate(String date, String format) {
        try {
            DateFormat formatter = new SimpleDateFormat(format);
            Date d = (Date) formatter.parse(date);
            return d;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取日期相差毫秒数
     *
     * @param subDate
     * @param minDate
     * @return
     */
    public static long calcDate(String subDate, String minDate) {
        long lSubDate = getCalendarByTime(subDate, "yyyy-MM-dd HH:mm:ss").getTimeInMillis();
        long lMinDate = getCalendarByTime(minDate, "yyyy-MM-dd HH:mm:ss").getTimeInMillis();
        return (lSubDate - lMinDate);
    }


    /**
     * 根据日期计算相差天数，忽略时分秒
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int calcDays(Date date1, Date date2) {
        Calendar date1Calendar = Calendar.getInstance();
        date1Calendar.setTime(date1);

        Calendar date2Calendar = Calendar.getInstance();
        date2Calendar.setTime(date2);

        int day1 = date1Calendar.get(Calendar.DAY_OF_YEAR);
        int day2 = date2Calendar.get(Calendar.DAY_OF_YEAR);

        return day1 - day2;
    }

    /**
     * 根据差值计算出新的时间
     *
     * @param time
     * @param seconds
     * @return
     */
    public static String calcDateByTime(String time, int seconds) {
        GregorianCalendar g = getCalendarByTime(time, "yyyy-MM-dd HH:mm:ss");
        return calcDate(g, seconds);
    }


    /**
     * 根据天数计算出新的日期
     *
     * @param date
     * @param days
     * @return
     */
    public static String calcNewDay(String date, int days) {
        GregorianCalendar g = new GregorianCalendar();
        g.setTimeInMillis(formatTime(date, "yyyy-MM-dd"));
        g.set(Calendar.DATE, g.get(Calendar.DATE) + days);
        return dateToStringSim(g);
    }

    /**
     * 下一天
     *
     * @param date
     * @return
     */
    public static String calcNextDay(String date) {
        GregorianCalendar g = new GregorianCalendar();
        g.setTimeInMillis(formatTime(date, "yyyy-MM-dd"));
        g.set(Calendar.DATE, g.get(Calendar.DATE) + 1);
        return dateToStringSim(g);
    }

    /**
     * 上一天
     *
     * @param date
     * @return
     */
    public static String calcLastDay(String date) {
        GregorianCalendar g = new GregorianCalendar();
        g.setTimeInMillis(formatTime(date, "yyyy-MM-dd"));
        g.set(Calendar.DATE, g.get(Calendar.DATE) - 1);
        return dateToStringSim(g);
    }

    /**
     * 上一天
     *
     * @return
     */
    public static String calcLastDay() {
        GregorianCalendar g = new GregorianCalendar();
        g.setTimeInMillis(formatTime(getCurTimeStr(), "yyyy-MM-dd"));
        g.set(Calendar.DATE, g.get(Calendar.DATE) - 1);
        return dateToStringSim(g);
    }

    private static Calendar getDateOfMonth(Calendar date, int num, boolean flag) {
        Calendar lastDate = (Calendar) date.clone();
        if (flag) {
            lastDate.add(Calendar.MONTH, num);
        } else {
            lastDate.add(Calendar.MONTH, -num);
        }

        return lastDate;
    }

    private static Calendar getDateOfLastMonth(String dateStr, int num, boolean flag) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = sdf.parse(dateStr);
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            return getDateOfMonth(c, num, flag);
        } catch (Exception e) {
            throw new IllegalArgumentException(
                    "Invalid date format(yyyyMMdd): " + dateStr);
        }
    }

    /**
     * 获取上个月的同一天
     *
     * @param date
     * @param num
     * @param flag
     * @return
     */
    public static String getSameDateOfLastMonth(String date, int num, boolean flag) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDate = sdf.format(getDateOfLastMonth(date, num, flag).getTime());
        return lastDate;
    }

    /**
     * GregorianCalendar转成字符串
     *
     * @param g
     * @return
     */
    private static String dateToString(GregorianCalendar g) {
        String year = String.valueOf(g.get(Calendar.YEAR));
        String month = String.format("%02d", g.get(Calendar.MONTH) + 1);
        String day = String.format("%02d", g.get(Calendar.DAY_OF_MONTH));
        String hours = String.format("%02d", g.get(Calendar.HOUR_OF_DAY));
        String minutes = String.format("%02d", g.get(Calendar.MINUTE));
        String seconds = String.format("%02d", g.get(Calendar.SECOND));
        return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
    }

    /**
     * GregorianCalendar转成字符串
     *
     * @param g
     * @return
     */
    private static String dateTString(GregorianCalendar g) {
        String year = String.valueOf(g.get(Calendar.YEAR));
        String month = String.format("%02d", g.get(Calendar.MONTH) + 1);
        String day = String.format("%02d", g.get(Calendar.DAY_OF_MONTH));
        return year + "-" + month + "-" + day;
    }

    /**
     * 根据时间转成MS
     *
     * @param time
     * @param format
     * @return
     */
    public static long stringToLong(String time, String format) {
        return stringToDate(time, format).getTime();
    }

    /**
     * 根据时间转成MS
     *
     * @param time
     * @return
     */
    public static long stringToLong(String time) {
        return stringToDate(time, "yyyy-MM-dd HH:mm:ss").getTime();
    }


    /**
     * GregorianCalendar转成日期格式
     *
     * @param g
     * @return
     */
    public static String dateToStringSim(GregorianCalendar g) {
        String year = String.valueOf(g.get(Calendar.YEAR));
        String month = String.format("%02d", g.get(Calendar.MONTH) + 1);
        String day = String.format("%02d", g.get(Calendar.DAY_OF_MONTH));
        return year + "-" + month + "-" + day;
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getCurTimeStr() {
        GregorianCalendar g = new GregorianCalendar();
        String year = String.valueOf(g.get(Calendar.YEAR));
        String month = String.format("%02d", g.get(Calendar.MONTH) + 1);
        String day = String.format("%02d", g.get(Calendar.DAY_OF_MONTH));
        String hours = String.format("%02d", g.get(Calendar.HOUR_OF_DAY));
        String minutes = String.format("%02d", g.get(Calendar.MINUTE));
        String seconds = String.format("%02d", g.get(Calendar.SECOND));

        return year + month + day + hours + minutes + seconds;
    }

    /**
     * 格式化时间
     *
     * @param time
     * @param oldFormat
     * @param newFormat
     * @return
     */
    public static String formatTime(String time, String oldFormat,
                                    String newFormat) {
        return new SimpleDateFormat(newFormat).format(stringToDate(time,
                oldFormat));
    }

    /**
     * 格式化日期
     *
     * @param date
     * @param format
     * @return
     */
    public static String formatTime(Date date, String format) {
        return new SimpleDateFormat(format).format(date);
    }

    /**
     * 格式化时间
     *
     * @param time
     * @param format
     * @return
     */
    public static String formatTime(long time, String format) {
        return formatTime(getTime(time), "yyyy-MM-dd HH:mm:ss", format);
    }

    /**
     * 获取时间, 根据格式化参数
     *
     * @param time
     * @param format
     * @return
     */
    public static long formatTime(String time, String format) {
        return stringToDate(time, format).getTime();
    }

    public static String dateForm(String date, String config) {
        if ("MM/dd/yyyy".equals(config)) {
            String[] dateStrs = date.split("/");
            return dateStrs[2] + "-" + dateStrs[0] + "-" + dateStrs[1];
        }
        if ("MM-dd-yyyy".equals(config)) {
            String[] dateStrs = date.split("-");
            return dateStrs[2] + "-" + dateStrs[0] + "-" + dateStrs[1];
        }
        return null;
    }

    /**
     * 比较两个时间大小
     *
     * @param beginTime
     * @param endTime
     * @return
     */
    public static boolean comparetoTime(String beginTime, String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //String beginTime = "2018-06-01";
        //String endTime = "2018-05-31";
        Date bt = null;
        Date et = null;
        try {
            bt = sdf.parse(beginTime);
            et = sdf.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (bt.before(et)) {
            //表示bt小于et
            System.out.println("true");
            return true;
        } else {
            //--反之 
            if (beginTime.equals(endTime)) {//相等
                System.out.println("true");
                return true;
            } else {
                System.out.println("false");
                return false;
            }

        }
    }

    //获取该时间的当天（结束时间）
    public static String today(String date) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date sampletime = null;
        try {
            sampletime = format.parse(date);   //sampletime为参数
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(sampletime);
        int dd = 6;
        c.set(Calendar.HOUR_OF_DAY, dd);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formatStr = sdf.format(c.getTime());

        return formatStr;
    }

    //获取当前当前时间的前一天
    public static String yesterdayStr(String date) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date sampletime = null;
        try {
            sampletime = format.parse(date);   //sampletime为参数
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(sampletime);
        int curHour24 = c.get(Calendar.HOUR_OF_DAY);
        int dd = 6;
        c.add(Calendar.DATE, -1);
        c.set(Calendar.HOUR_OF_DAY, dd);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formatStr = sdf.format(c.getTime());

        return formatStr;
    }

    //获取前两天的时间
    public static String queryYesterdayStr(String date) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date sampletime = null;
        try {
            sampletime = format.parse(date);   //sampletime为参数
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(sampletime);
        int curHour24 = c.get(Calendar.HOUR_OF_DAY);
        int dd = 6;
        c.add(Calendar.DATE, -2);
        c.set(Calendar.HOUR_OF_DAY, dd);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formatStr = sdf.format(c.getTime());

        return formatStr;
    }

    //获取当前时间的明天(结束时间)
    public static String tomorroStr(String date) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date sampletime = null;
        try {
            sampletime = format.parse(date);   //sampletime为参数
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(sampletime);
        int curHour24 = c.get(Calendar.HOUR_OF_DAY);
        int dd = 6;
        c.add(Calendar.DATE, +1);
        c.set(Calendar.HOUR_OF_DAY, dd);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formatStr = sdf.format(c.getTime());

        return formatStr;
    }


    //获取当前时间的明天(结束时间)
    public static String tomorro() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        int curHour24 = c.get(Calendar.HOUR_OF_DAY);
        int dd = 6;
        c.add(Calendar.DATE, +1);
        c.set(Calendar.HOUR_OF_DAY, dd);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(c.getTime());
        return format;
    }

    //获取当前当前时间的前一天
    public static String yesterday() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        int curHour24 = c.get(Calendar.HOUR_OF_DAY);
        int dd = 6;
        c.add(Calendar.DATE, -1);
        c.set(Calendar.HOUR_OF_DAY, dd);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(c.getTime());
        return format;
    }

    //获取前两天的时间
    public static String queryYesterday() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        int curHour24 = c.get(Calendar.HOUR_OF_DAY);
        int dd = 6;
        c.add(Calendar.DATE, -2);
        c.set(Calendar.HOUR_OF_DAY, dd);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(c.getTime());
        return format;
    }


    /**
     * LocalDate转Date
     *
     * @param localDate
     * @return
     */
    public static Date localDate2Date(LocalDate localDate) {
        if (null == localDate) {
            return null;
        }
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }

    /**
     * Date转LocalDate
     *
     * @param date
     */
    public static LocalDate date2LocalDate(Date date) {
        if (null == date) {
            return null;
        }
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }

    /**
     * Date转换为LocalDateTime
     *
     * @param date
     */
    public static LocalDateTime date2LocalDateTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        return localDateTime;
    }

    /**
     * LocalDateTime转换为Date
     *
     * @param localDateTime
     */
    public static Date localDateTime2Date(LocalDateTime localDateTime) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);
        Date date = Date.from(zdt.toInstant());
        return date;
    }


    public static int getYear(String time) {
        return Integer.parseInt(time.substring(0, 4));
    }

    public static int getMonth(String time) {
        return Integer.parseInt(time.substring(5, 7));
    }

    public static int getDay(String time) {
        return Integer.parseInt(time.substring(8, 10));
    }

    public static int getHours(String time) {
        return Integer.parseInt(time.substring(11, 13));
    }

    public static int getMinutes(String time) {
        return Integer.parseInt(time.substring(14, 16));
    }

    public static int getSeconds(String time) {
        return Integer.parseInt(time.substring(17));
    }

    public static int getYear() {
        GregorianCalendar g = new GregorianCalendar();
        return g.get(Calendar.YEAR);
    }

    public static int getMonth() {
        GregorianCalendar g = new GregorianCalendar();
        return g.get(Calendar.MONTH) + 1;
    }

    public static int getDay() {
        GregorianCalendar g = new GregorianCalendar();
        return g.get(Calendar.DAY_OF_MONTH);
    }

    public static int getHours() {
        GregorianCalendar g = new GregorianCalendar();
        return g.get(Calendar.HOUR_OF_DAY);
    }

    public static int getMinutes() {
        GregorianCalendar g = new GregorianCalendar();
        return g.get(Calendar.MINUTE);
    }

    public static int getSeconds() {
        GregorianCalendar g = new GregorianCalendar();
        return g.get(Calendar.SECOND);
    }

    public static String getBeforeTimeByMinute(int minute) {
        GregorianCalendar g = new GregorianCalendar();
        g.set(Calendar.MINUTE, g.get(Calendar.MINUTE) - minute);
        return dateToString(g);
    }

    // 获取昨天的结束时间
    public static Date getEndDayOfYesterDay() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(get6HourDays());
        cal.add(Calendar.SECOND, -1);
        return cal.getTime();
    }

    // 获取开始的结束时间
    public static Date getStartDayOfYesterDay() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(get6HourDays());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }

    // 获取今年是哪一年
    public static Integer getNowYear() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return Integer.valueOf(gc.get(1));
    }

    // 获取本月是哪一月
    public static int getNowMonth() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return gc.get(2) + 1;
    }

    // 获取前天的结束时间
    public static Date getEndDayOfYesterDayBefore() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(get6HourDays());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        cal.add(Calendar.SECOND, -1);
        return cal.getTime();
    }

    // 获取前天的开始时间
    public static Date getStartDayOfYesterDayBefore() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(get6HourDays());
        cal.add(Calendar.DAY_OF_MONTH, -2);
        return cal.getTime();
    }

    // 获取本周开始时间
    public static Date getBeginDayOfWeek() {
        Date date = get6HourDays();
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek);
        return getDayStartTime(cal.getTime());
    }

    // 获取某个日期的开始时间  6点开始算
    public static Timestamp getDayStartTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d)
            calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), 6, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new Timestamp(calendar.getTimeInMillis());
    }

    // 获取这个月开始的那天
    public static Date getBeginDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 1, 1);
        return getDayStartTime(calendar.getTime());
    }

}
